package com.beeantec.jnrerc.modules.repo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.api.dto.BaseIdReqDTO;
import com.beeantec.jnrerc.core.api.dto.BaseIdsReqDTO;
import com.beeantec.jnrerc.core.api.dto.PagingReqDTO;
import com.beeantec.jnrerc.core.enums.QuType;
import com.beeantec.jnrerc.core.utils.BeanMapper;

import com.beeantec.jnrerc.dto.QuRepoBatchReqDTO;
import com.beeantec.jnrerc.dto.RepoDTO;
import com.beeantec.jnrerc.dto.response.RepoRespDTO;

import com.beeantec.jnrerc.entity.Qu;
import com.beeantec.jnrerc.entity.QuRepo;
import com.beeantec.jnrerc.entity.Repo;
import com.beeantec.jnrerc.modules.qu.service.QuAnswerService;
import com.beeantec.jnrerc.modules.qu.service.QuRepoService;
import com.beeantec.jnrerc.modules.qu.service.QuService;
import com.beeantec.jnrerc.modules.repo.service.RepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
* <p>
* 题库控制器
* </p>
*
* @author Anthony
* @since 2020-05-25 13:25
*/
@Api(tags={"题库"})
@RestController
@RequestMapping("/api/repo")
public class RepoController extends BaseController {

    @Autowired
    private RepoService baseService;

    @Autowired
    private QuRepoService quRepoService;




    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest save(@RequestBody RepoDTO reqDTO) {
        reqDTO.setCreateTime(new Date());
        baseService.save(reqDTO);
        return super.success();
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        if (baseService.removeByIds(reqDTO.getIds())){
            return super.success("删除成功");
        }else {
            return super.failure("删除失败");
        }
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiRest<RepoDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        baseService.refreshCount();
        Repo entity = baseService.getById(reqDTO.getId());
        if (null==entity){
            return failure("没有该题库");
        }
        RepoDTO dto = new RepoDTO();
        BeanUtils.copyProperties(entity, dto);
//        QueryWrapper<QuRepo> juQuRepoQueryWrapper = new QueryWrapper<>();
//        juQuRepoQueryWrapper.lambda().eq(QuRepo::getRepoId,reqDTO.getId()).eq(QuRepo::getQuType, QuType.JUDGE);
//        QueryWrapper<QuRepo> raQuRepoQueryWrapper = new QueryWrapper<>();
//        raQuRepoQueryWrapper.lambda().eq(QuRepo::getRepoId,reqDTO.getId()).eq(QuRepo::getQuType, QuType.RADIO);
//        QueryWrapper<QuRepo> muQuRepoQueryWrapper = new QueryWrapper<>();
//        muQuRepoQueryWrapper.lambda().eq(QuRepo::getRepoId,reqDTO.getId()).eq(QuRepo::getQuType, QuType.MULTI);
//        dto.setJudgeCount(quRepoService.count(juQuRepoQueryWrapper));
//        dto.setMultiCount(quRepoService.count(muQuRepoQueryWrapper));
//        dto.setRadioCount(quRepoService.count(raQuRepoQueryWrapper));
        return super.success(dto);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<RepoRespDTO>> paging(@RequestBody PagingReqDTO<RepoDTO> reqDTO) {
        baseService.refreshCount();
        //分页查询并转换
        IPage<RepoRespDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }

    /**
     * 批量操作
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "批量操作", notes = "批量加入或从题库移除")
    @RequestMapping(value = "/batch-action", method = { RequestMethod.POST})
    public ApiRest paging(@RequestBody QuRepoBatchReqDTO reqDTO) {
        baseService.refreshCount();
        //分页查询并转换
        quRepoService.batchAction(reqDTO);
        return super.success();
    }

    /**
     * 查找列表，每次最多返回200条数据
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/list", method = { RequestMethod.POST})
    public ApiRest<List<RepoDTO>> list(@RequestBody RepoDTO reqDTO) {
        baseService.refreshCount();
        //分页查询并转换
        QueryWrapper<Repo> wrapper = new QueryWrapper<>();

        //转换并返回
        List<Repo> list = baseService.list(wrapper);
//        for (Repo repo:list){
//            QueryWrapper<QuRepo> juQuRepoQueryWrapper = new QueryWrapper<>();
//            juQuRepoQueryWrapper.lambda().eq(QuRepo::getRepoId,reqDTO.getId()).eq(QuRepo::getQuType, QuType.JUDGE);
//            QueryWrapper<QuRepo> raQuRepoQueryWrapper = new QueryWrapper<>();
//            raQuRepoQueryWrapper.lambda().eq(QuRepo::getRepoId,reqDTO.getId()).eq(QuRepo::getQuType, QuType.RADIO);
//            QueryWrapper<QuRepo> muQuRepoQueryWrapper = new QueryWrapper<>();
//            muQuRepoQueryWrapper.lambda().eq(QuRepo::getRepoId,reqDTO.getId()).eq(QuRepo::getQuType, QuType.MULTI);
//            repo.setJudgeCount(quRepoService.count(juQuRepoQueryWrapper));
//            repo.setMultiCount(quRepoService.count(muQuRepoQueryWrapper));
//            repo.setRadioCount(quRepoService.count(raQuRepoQueryWrapper));
//        }

        //转换数据
        List<RepoDTO> dtoList = BeanMapper.mapList(list, RepoDTO.class);

        return super.success(dtoList);
    }
}
